package com.yxuan.springboot.controller;

import com.github.pagehelper.PageInfo;
import com.yxuan.springboot.model.BaseSchoolInformation;
import com.yxuan.springboot.service.SchoolInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import com.yxuan.springboot.model.SchoolInformation;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.*;

/**
 * 
 * @author zchao
 *   SchoolInformation控制器
 * @date 2023-05-09 11:17:05
 */
@Controller
@Api(tags = "院校信息表")
@RequestMapping(value = "/schoolinformation", method = {RequestMethod.POST,RequestMethod.GET})
@CacheConfig(cacheNames = "schoolinformation")//缓存名，和管理器中配置的一致
public class SchoolInformationController extends BaseController<SchoolInformation>{

    @Autowired
    private SchoolInformationService schoolInformationService;

    /**
     * 加载（分页） 根据是否存在条件加载--学校基础信息
     */
    @ApiOperation(value = "根据是否存在条件分页查询（常用）--学校基础信息", notes = "根据是否存在条件分页查询（常用）--学校基础信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "起始页码",
                    defaultValue = "1", required = true,
                    dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页记录条数",
                    defaultValue = "10", required = true,
                    dataType = "int", paramType = "query")
    })
    @RequestMapping("/pageBaseByPramas")
    @ResponseBody
    @Cacheable(key = "'pageBaseByPramas-' + #page + #limit + #provinces + #natures +#gradations +#schoolTypes " +
            "+#eryaoyao +#jiubawu +#doubleFirst +#schoolName",
            unless = "#result==null",
            condition = "#page!=null && #limit != null")//#result返回结果
    public PageInfo<BaseSchoolInformation> pageBaseByPramas(Integer page, Integer limit,
                                                            String provinces,
                                                            String natures,
                                                            String gradations,
                                                            String schoolTypes,
                                                            String eryaoyao,
                                                            String jiubawu,
                                                            String doubleFirst,
                                                            String schoolName){
        System.out.println("page="+page+"limit="+limit);
        try {
            List<String> provinceList = null;
            if (provinces != null){
                String[] provinceArray = provinces.split(",");
                provinceList = Arrays.asList(provinceArray);
            }
            List<String> natureList = null;
            if (natures != null){
                String[] natureArray = natures.split(",");
                natureList = Arrays.asList(natureArray);
            }
            List<String> gradationList = null;
            if (gradations != null){
                String[] gradationArray = gradations.split(",");
                gradationList = Arrays.asList(gradationArray);
            }
            List<String> schoolTypeList = null;
            if (schoolTypes != null){
                String[] schoolTypeArray = schoolTypes.split(",");
                schoolTypeList = Arrays.asList(schoolTypeArray);
            }
            System.out.println(provinceList);
            System.out.println(natureList);
            System.out.println(gradationList);
            System.out.println(schoolTypeList);
            return schoolInformationService.findBaseSchoolInformation(page,limit,
                    provinceList,natureList,gradationList,schoolTypeList,eryaoyao,jiubawu,doubleFirst,schoolName);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取分组数据
     */
    @ApiOperation(value = "获取分组数据--省份", notes = "获取分组数据--省份")
    @RequestMapping("/loadProvinceByGroup")
    @ResponseBody
    @Cacheable(key = "'loadProvinceByGroup'")//#result返回结果
    public ArrayList<Map<String, Object>> loadProvinceByGroup() {
        List<SchoolInformation> list = schoolInformationService.findProvinceByGroup();
        ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
        for (SchoolInformation schoolInformation:list){
            Map<String,Object> map = new HashMap<>();
            map.put("id",schoolInformation.getId());
            map.put("active",0);
            map.put("name",schoolInformation.getProvince());
            arrayList.add(map);
        }
        return arrayList;
    }

    /**
     * 获取分组数据
     */
    @ApiOperation(value = "获取分组数据--办学层次", notes = "获取分组数据--办学层次")
    @RequestMapping("/loadGradationByGroup")
    @ResponseBody
    @Cacheable(key = "'loadGradationByGroup'")//#result返回结果
    public ArrayList<Map<String, Object>> loadGradationByGroup() {
        List<SchoolInformation> list = schoolInformationService.findGradationByGroup();
        ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
        for (SchoolInformation schoolInformation:list){
            Map<String,Object> map = new HashMap<>();
            map.put("id",schoolInformation.getId());
            map.put("active",0);
            map.put("name",schoolInformation.getGradation());
            arrayList.add(map);
        }
        return arrayList;
    }

    /**
     * 获取分组数据
     */
    @ApiOperation(value = "获取分组数据--办学性质", notes = "获取分组数据--办学性质")
    @RequestMapping("/loadNatureByGroup")
    @ResponseBody
    @Cacheable(key = "'loadNatureByGroup'")//#result返回结果
    public ArrayList<Map<String, Object>> loadNatureByGroup() {
        List<SchoolInformation> list = schoolInformationService.findNatureByGroup();
        ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
        for (SchoolInformation schoolInformation:list){
            Map<String,Object> map = new HashMap<>();
            map.put("id",schoolInformation.getId());
            map.put("active",0);
            map.put("name",schoolInformation.getNature());
            arrayList.add(map);
        }
        return arrayList;
    }

    /**
     * 获取分组数据
     */
    @ApiOperation(value = "获取分组数据--学校类型", notes = "获取分组数据--学校类型")
    @RequestMapping("/loadSchoolTypeByGroup")
    @ResponseBody
    @Cacheable(key = "'loadSchoolTypeByGroup'")//#result返回结果
    public ArrayList<Map<String, Object>> loadSchoolTypeByGroup() {
        List<SchoolInformation> list = schoolInformationService.findSchoolTypeByGroup();
        ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
        for (SchoolInformation schoolInformation:list){
            Map<String,Object> map = new HashMap<>();
            map.put("id",schoolInformation.getId());
            map.put("active",0);
            map.put("name",schoolInformation.getSchoolType());
            arrayList.add(map);
        }
        return arrayList;
    }

    /**
     * 根据专业id获取关联院校
     */
    @ApiOperation(value = "根据专业id获取关联院校", notes = "根据专业id获取关联院校")
    @RequestMapping("/loadByProfessionalID")
    @ResponseBody
    @Cacheable(key = "'loadByProfessionalID-' + #page + #limit + #professionalID",
            unless = "#result==null",
            condition = "#page!=null && #limit != null && #professionalID != null")//#result返回结果
    public PageInfo<BaseSchoolInformation> loadByProfessionalID(Integer page, Integer limit, Long professionalID) {
        return schoolInformationService.findByProfessionalID(page,limit,professionalID);
    }

    @RequestMapping("/changefilename")
    public void changeFileName() throws Exception {
        List list1 = new ArrayList<>();
        File file = new File("D:/桌面/图片");
        File[] list = file.listFiles();
//        for (File file1:list){
//            File dest = new File("D:/桌面" + "/学校图片/" + ;
//            file1.renameTo(dest);
//            System.out.println(dest);
//        }
        for (File file1:list){
            String name = file1.getName();
            System.out.println(name);
            SchoolInformation schoolInformation = new SchoolInformation();
            schoolInformation.setSchoolName(name);
            if (schoolInformationService.findObjectByPramas(schoolInformation) == null){
                list1.add(name);
                continue;
            }
            SchoolInformation objectByPramas = schoolInformationService.findObjectByPramas(schoolInformation);
            String new_name = String.valueOf(objectByPramas.getId());
            File dest = new File("D:/桌面" + "/图片/" + new_name);
            file1.renameTo(dest);
        }
        System.out.println(list1);
    }

    /**
     * ids查询院校信息
     */
    @ApiOperation(value = "ids查询院校信息", notes = "ids查询院校信息")
    @RequestMapping("/findSchoolInfoByIdMoreInfor")
    @ResponseBody
    public List<SchoolInformation> findSchoolInfoByIdMoreInfor(String[] item) {
        System.out.println(Arrays.toString(item));
        return schoolInformationService.getSchoolInfoByIdMoreInfor(Arrays.asList(item));
    }

    /**
     * 根据条件查询单个结果
     *
     */
    @ApiOperation(value = "根据条件查询单个数据", notes = "根据条件查询单个数据",httpMethod = "GET")
    @RequestMapping("/loadObjectByPramas")
    @ResponseBody
    @Cacheable(key = "'schoolloadObjectByPramas-' + #t",
            unless = "#result==null",
            condition = "#t!=null")//#result返回结果
    public SchoolInformation loadObjectByPramas(SchoolInformation t){
        try {
            return schoolInformationService.findObjectByPramas(t);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
